/*
 * Copyright 2005-2013 biz-united. All rights reserved.
 * Support: http://www.biz-united.com.cn
 * License: http://www.biz-united.com.cn/?page_id=5&act=3
 */
package net.biz.controller.admin;

import com.home766.util.poi.excel.BrowserUtils;
import com.home766.util.poi.excel.ExcelExportUtil;
import com.home766.util.poi.excel.entity.vo.ExcelTitle;
import net.biz.Message;
import net.biz.Pageable;
import net.biz.dto.RecordDto;
import net.biz.entity.Admin;
import net.biz.entity.AdminRecord;
import net.biz.service.AdminService;
import net.biz.service.RecordService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

/**
 * Controller - 备案表
 * 
 * @version 3.0
 */
@Controller
@RequestMapping("/admin/record")
public class RecordController extends BaseController {


	@Resource(name = "recordServiceImpl")
	private RecordService recordServiceImpl;

	@Resource(name = "adminServiceImpl")
	private AdminService adminService;


	@RequestMapping("/exportXlsRecordInfo")
	public void exportXlsRecordInfo( Date beginDate, Date endDate, HttpServletRequest request, HttpServletResponse response){
        Admin admin = getCurrent();
        if(admin.getIsSystem()){
            admin = null;
        }

		String codedFileName = "导出报备表信息";
		OutputStream fOut = null;
		Pageable pagebale = new Pageable();
		List<RecordDto> exporList= recordServiceImpl.getRecordDtoList(admin,beginDate,endDate,pagebale);
        System.out.println(exporList);
        response.setContentType("application/vnd.ms-excel");
		try {
			// 根据浏览器进行转码，使其支持中文文件名
			if (BrowserUtils.isIE(request)) {
				response.setHeader(
						"content-disposition",
						"attachment;filename="
								+ java.net.URLEncoder.encode(codedFileName,
								"UTF-8") + ".xls");
			} else {
				String newtitle = new String(codedFileName.getBytes("UTF-8"),
						"ISO8859-1");
				response.setHeader("content-disposition",
						"attachment;filename=" + newtitle + ".xls");
			}
			// 产生工作簿对象
			HSSFWorkbook workbook = null;
			workbook = ExcelExportUtil.exportExcel(new ExcelTitle(codedFileName, "导出人:"+adminService.getCurrentUsername(),
					"导出信息"), RecordDto.class, exporList);
			fOut = response.getOutputStream();
			workbook.write(fOut);
		} catch (Exception e) {
		} finally {
			try {
				fOut.flush();
				fOut.close();
			} catch (IOException e) {

			}
		}
	}
	
	/**
	 * 查询备案表列表
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Pageable pageable, Date beginDate, Date endDate, ModelMap model) {
		Admin admin = getCurrent();
		if(admin.getIsSystem()){
            admin = null;
		}
        model.addAttribute("page", recordServiceImpl.findPage(admin,beginDate,endDate,pageable));
		return "/admin/record/list";
	}

	/**
	 * 添加备案表表单
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String form(ModelMap model) {
		return "/admin/record/add";
	}
	
	/**
	 * 备案表编辑
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(Long id, ModelMap model) {
		model.addAttribute("adminRecord", recordServiceImpl.find(id));
		return "/admin/record/edit";
	}

	/**
	 * 更新
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(AdminRecord adminRecord, RedirectAttributes redirectAttributes) {
		adminRecord.setAdmin(getCurrent());
		if (!isValid(adminRecord)) {
			return ERROR_VIEW;
		}
//		if(adminRecord.getCompanyName()==null||adminRecord.getRegisterAddr()==null||adminRecord.getOfficeAddr()==null)
//			return ERROR_VIEW;
		
		recordServiceImpl.update(adminRecord);
		addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
		return "redirect:list.jhtml";
	}
	

	/**
	 * 保存备案表
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(AdminRecord adminRecord,String system_text) {
		if(system_text!=null && adminRecord.getSystem().equals("7")){
			adminRecord.setSystem(system_text);
		}
		adminRecord.setAdmin(getCurrent());
//		System.out.println(adminRecord.toString());
		recordServiceImpl.save(adminRecord);
		return "redirect:list.jhtml";
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody
	Message delete(Long[] ids) {
		recordServiceImpl.delete(ids);
		return SUCCESS_MESSAGE;
	}

}